package me.leiusual.sharing.repository

import androidx.lifecycle.LiveData
import androidx.paging.Config
import androidx.paging.PagedList
import androidx.paging.toLiveData
import io.reactivex.Single
import me.leiusual.sharing.api.ApiOauth2
import me.leiusual.sharing.api.ApiSharing
import me.leiusual.sharing.api.model.StoreCategories
import me.leiusual.sharing.api.model.StoreDetail
import me.leiusual.sharing.api.model.storeindex.StoreIndex
import me.leiusual.sharing.api.model.storeproductquery.Product
import me.leiusual.sharing.api.model.storesearch.StoreContent
import me.leiusual.sharing.repository.datasource.ProductDataSourceFactory
import me.leiusual.sharing.repository.datasource.StoreSearchDataSourceFactory
import javax.inject.Inject
import javax.inject.Singleton

/**
 * author: leiusual
 * create on: 2019-07-25
 * description:
 */
@Singleton
class StoreRepository @Inject constructor(
    private val apiSharing: ApiSharing
) {

  fun storeApply(name: String): Single<Unit> {
    return apiSharing.storeApply(name)
  }

  fun storeSearch(keyword: String): LiveData<PagedList<StoreContent>> {
    return StoreSearchDataSourceFactory(apiSharing, keyword).toLiveData(Config(9, initialLoadSizeHint = 9))
  }

  fun storeCategories(parentCategoryId: Long?, storeId: Long): Single<StoreCategories.Response> {
    return apiSharing.storeCategories(StoreCategories.Request(ApiOauth2.CLIENT_ID, parentCategoryId, storeId))
  }

  fun storeDetail(storeId: Long): Single<StoreDetail.Response> {
    return apiSharing.storeDetail(StoreDetail.Request(ApiOauth2.CLIENT_ID, storeId))
  }

  fun storeIndex(bindStoreId: Long, storeId: Long): Single<StoreIndex> {
    return apiSharing.storeIndex(bindStoreId, storeId)
  }

  fun storeProductQuery(storeId: Long, storeAreaId: Long, categoryId: Long): LiveData<PagedList<Product>> {
    return ProductDataSourceFactory(apiSharing, storeId, storeAreaId, categoryId).toLiveData(Config(9, initialLoadSizeHint = 9))
  }
}